package com.train.user.controller;

import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.core.domain.model.LoginUser;
import com.train.common.utils.SecurityUtils;
import com.train.user.service.SysUserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户关注管理 控制器
 * 
 * @author train
 */
@RestController
@RequestMapping("/user/follow")
public class SysUserFollowController extends BaseController {

    @Autowired
    private SysUserFollowService sysUserFollowService;

    /**
     * 关注用户
     * 
     * @param followUserId 被关注用户ID
     * @return 结果
     */
    @PostMapping("/{followUserId}")
    public AjaxResult followUser(@PathVariable("followUserId") Long followUserId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        boolean result = sysUserFollowService.followUser(userId, followUserId);
        return result ? success("关注成功") : error("关注失败或已关注该用户");
    }

    /**
     * 取消关注
     * 
     * @param followUserId 被关注用户ID
     * @return 结果
     */
    @DeleteMapping("/{followUserId}")
    public AjaxResult unfollowUser(@PathVariable("followUserId") Long followUserId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        boolean result = sysUserFollowService.unfollowUser(userId, followUserId);
        return result ? success("取消关注成功") : error("取消关注失败");
    }

    /**
     * 检查是否已关注
     * 
     * @param followUserId 被关注用户ID
     * @return 结果
     */
    @GetMapping("/check/{followUserId}")
    public AjaxResult checkFollowing(@PathVariable("followUserId") Long followUserId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        boolean isFollowing = sysUserFollowService.isFollowing(userId, followUserId);
        return success(isFollowing);
    }

    /**
     * 获取我的关注数量
     * 
     * @return 结果
     */
    @GetMapping("/following/count")
    public AjaxResult getFollowingCount() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        long count = sysUserFollowService.getFollowingCount(userId);
        return success(count);
    }

    /**
     * 获取指定用户的关注数量
     * 
     * @param userId 用户ID
     * @return 结果
     */
    @GetMapping("/following/count/{userId}")
    public AjaxResult getFollowingCountByUserId(@PathVariable("userId") Long userId) {
        long count = sysUserFollowService.getFollowingCount(userId);
        return success(count);
    }

    /**
     * 获取我的粉丝数量
     * 
     * @return 结果
     */
    @GetMapping("/follower/count")
    public AjaxResult getFollowerCount() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        long count = sysUserFollowService.getFollowerCount(userId);
        return success(count);
    }

    /**
     * 获取指定用户的粉丝数量
     * 
     * @param userId 用户ID
     * @return 结果
     */
    @GetMapping("/follower/count/{userId}")
    public AjaxResult getFollowerCountByUserId(@PathVariable("userId") Long userId) {
        long count = sysUserFollowService.getFollowerCount(userId);
        return success(count);
    }
}
